package com.ahao.aggregation;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.PopupWindow;

import com.ahao.aggregation.dialog.CustomPersonalDialog;
import com.ahao.aggregation.utils.LogUtils;
import com.ahao.aggregation.utils.ScreenUtils;
import com.ahao.aggregation.utils.ZHUiUtils;

public class FloatViewManager {
    private static FloatViewManager instance;
    private ZHFloatView floatView;

    public static FloatViewManager getInstance() {
        if(instance == null) {
            instance = new FloatViewManager();
        }
        return instance;
    }

    @SuppressLint("ClickableViewAccessibility")
    public void showFloatView(Context context) {
        LogUtils.info("显示聚合层浮窗");
        if (floatView ==null) {
            floatView = ZHUiUtils.addZHView(context, View.inflate(context, MResource.getIdByName(context, "layout", "layout_ahaosdk_floatview_window"), null), new IAddZHViewCallback() {
                @Override
                public FrameLayout.LayoutParams addViewBefore(FrameLayout.LayoutParams layoutParams) {
                    layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                    layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    layoutParams.gravity = Gravity.LEFT | Gravity.CENTER;
                    return layoutParams;
                }
            });
            DisplayMetrics displayMetrics = ScreenUtils.getDisplayMetrics(context);
            floatView.initFloatEvent(displayMetrics.widthPixels, displayMetrics.heightPixels);
            floatView.setItemOnclickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LogUtils.info("点击了聚合层浮窗");
                    // 打开个人中心弹窗
                    CustomPersonalDialog.showPersonalDialog(view.getContext());
                }
            });
        }else {
            floatView.showItem();
        }
    }

    public void hideFloatView() {
        LogUtils.info("隐藏聚合层浮窗");
        if (floatView != null) {
            floatView.hideItem();
        }
    }
}